package view.GUI.sidepanels;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

/**
 *
 * @author Ben
 */
public class BooleanEditor  extends AbstractCellEditor
                         implements TableCellEditor,
                                    ActionListener {
    Boolean visible;
    JCheckBox check;

    public BooleanEditor() {
        check = new JCheckBox();
        check.setActionCommand("EDIT");
        check.addActionListener(this);
        check.setBorderPainted(false);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("EDIT")) {
            visible = !visible;
            check.setSelected(visible);
            fireEditingStopped(); //Make the renderer reappear.
        }
    }

    //Implement the one CellEditor method that AbstractCellEditor doesn't.
    public Object getCellEditorValue() {
        return visible;
    }

    //Implement the one method defined by TableCellEditor.
    public Component getTableCellEditorComponent(JTable table,
                                                 Object value,
                                                 boolean isSelected,
                                                 int row,
                                                 int column) {
        visible = (Boolean)value;
        return check;
    }
}